package com.skyvin.nreceipts.receipts.service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.skyvin.nreceipts.common.document.ReceiptUpload;
import com.skyvin.nreceipts.common.service.BaseService;
import com.skyvin.nreceipts.receipts.mongorepository.ReceiptUploadRepository;
import com.skyvin.nreceipts.user.service.UserService;

@Service
public class ReceiptUploadService extends BaseService {
	private Logger logger = Logger.getLogger(ReceiptUploadService.class);
	
	@Autowired
	ReceiptUploadRepository receiptUploadRepository;
	@Autowired
	UserService userService;
	
	@Autowired
	MongoTemplate mongoTemplate; 

	/**
	 * This method uploads the file and saves the receipt in the mongodb
	 * 
	 * @param receiptUpload
	 * @return
	 * @throws IOException
	 */
	@Transactional
	public ReceiptUpload uploadFileandSaveReceipt(ReceiptUpload receiptUpload)
			throws IOException {
		ReceiptUpload updatedReceiptUpload = null;
		try {
			// uploading file
			String receiptURL = uploadFile(receiptUpload.getReceiptFile());

			// Setting receipt attributes
			receiptUpload.setReceiptURL(receiptURL);
			receiptUpload.setUserPID(userService.getCurrentUserPID());
			receiptUpload.setUploadedDate(new Date());

			// saving receipt in mongodb.
			updatedReceiptUpload = save(receiptUpload);
		} catch (IOException ioException) {
			logger.error("Error in uploading file : " + ioException);
			throw ioException;
		}
		return updatedReceiptUpload;
	}
	
	/**
	 * saves the receipt upload document in mongodb.
	 * 
	 * @param receiptUpload
	 * @return
	 */
	
	@Transactional
	public ReceiptUpload save(ReceiptUpload receiptUpload) {
		receiptUploadRepository.save(receiptUpload);
		mongoTemplate.save(receiptUpload, getMyCollection(ReceiptUpload.class));
		ReceiptUpload myReceiptUploadCreated = mongoTemplate.findOne(new Query(Criteria.where("receiptURL").is(receiptUpload.getReceiptURL())), ReceiptUpload.class, getMyCollection(ReceiptUpload.class));
		logger.info("ID Generated : " + myReceiptUploadCreated.getId());
		return myReceiptUploadCreated;
	}

	@Transactional
	public void deleteReceiptUpload(String receiptUploadPID) {
		//receiptUploadRepository.delete(receiptUploadPID);
		
		Query query = new Query(Criteria.where("id").is(receiptUploadPID));
		mongoTemplate.findAndRemove(query, ReceiptUpload.class, getMyCollection(ReceiptUpload.class));
	}

	/**
	 * This method uploads the file into the file repository.
	 * 
	 * @param commonsMultipartFile
	 * @return
	 * @throws IOException
	 */
	@Transactional
	public String uploadFile(CommonsMultipartFile commonsMultipartFile)
			throws IOException {
		String fileURL = "resources/images/receipts/"
				+ commonsMultipartFile.getOriginalFilename();
		File file = new File(
				"C:/nReceipts/nReceiptsWorkspace/nreceipts/src/main/nreceipts.war/"
						+ fileURL);
		commonsMultipartFile.transferTo(file);
		return fileURL;
	}

	public List<ReceiptUpload> retrieveUploadedReceipts() {
		List<ReceiptUpload> receiptUploads = mongoTemplate.findAll(ReceiptUpload.class, getMyCollection(ReceiptUpload.class));
		return receiptUploads;
	}
	public ReceiptUpload getMyReceiptUpload(String receiptUploadId) {
		Query query = new Query(Criteria.where("id").is(receiptUploadId));
		ReceiptUpload receiptUpload = mongoTemplate.findOne(query, ReceiptUpload.class, getMyCollection(ReceiptUpload.class));
		return receiptUpload;
	}
	
	public void mergeImages() {
		logger.info("Starting to merge images");
		logger.info("Completed merging images...");
	}

}
